package com.joven.shelltest.controller;

import com.joven.shelltest.utils.hotsearch.HotSearchUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: wzj
 * @Date: 2024/2/20 11:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/search")
public class SearchHistoryController {
    
    private final HotSearchUtil hotSearchUtil;


    @GetMapping("/add")
    public String addSearchHistoryByUserId(String userId, String searchKey) {
        hotSearchUtil.addSearchHistoryByUserId(userId, searchKey);
        hotSearchUtil.incrementScore(searchKey);
        return null;
    }

    /**
     * 删除个人历史数据
     */
    @GetMapping("/del")
    public Long delSearchHistoryByUserId(String userId, String searchKey) {
        return hotSearchUtil.delSearchHistoryByUserId(userId, searchKey);
    }

    /**
     * 获取个人历史数据列表
     */
    @GetMapping("/getUser")
    public List<String> getSearchHistoryByUserId(String userId) {
        return hotSearchUtil.getSearchHistoryByUserId(userId);
    }



    /**
     * 根据searchKey搜索其相关最热的前十名 (如果searchKey为null空，则返回redis存储的前十最热词条)
     */
    @GetMapping("/getHot")
    public List<String> getHotList(String searchKey) {
        return hotSearchUtil.getHotList(searchKey);
    }
}
